package InstanceZone

import (
	codecs "sanguo/codec/cs"
	"sanguo/node/common/DropPool"
	"sanguo/node/common/com_type"
	"sanguo/node/common/transaction"
	"sanguo/node/node_game"
	"sanguo/node/node_game/module/role"
	"sanguo/node/node_game/user"
	cs_message "sanguo/protocol/cs/message"
	"time"

	"github.com/golang/protobuf/proto"
)

type transactionEndInstanceZone struct {
	transaction.TransactionBase
	user    *user.User
	req     *codecs.Message
	resp    *cs_message.EndInstanceZoneToC
	errcode cs_message.EnumType
}

func (this *transactionEndInstanceZone) GetModuleName() string {
	return "InstanceZone"
}

func (this *transactionEndInstanceZone) GetReplyMsg() proto.Message {
	return &cs_message.EndInstanceZoneToC{}
}

func (this *transactionEndInstanceZone) SetExpiredTime(expired time.Time) {
	this.Expired = expired
}

func (this *transactionEndInstanceZone) GetExpiredTime() time.Time {
	return this.Expired
}

func (this *transactionEndInstanceZone) Begin() {
	msg := this.req.GetData().(*cs_message.EndInstanceZoneToS)
	this.resp = &cs_message.EndInstanceZoneToC{}
	roleModule := this.user.GetModuleByName("role").(*role.RoleModule)
	role := roleModule.CurrentRole()

	if role == nil || !role.IsRoleModleData() {
		node_game.Infoln(this.user.GetUserID(), "EndInstanceZone fail : no selectRole ")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}
	node_game.Infof("%s %d Call EndInstanceZone IsFinished:%v", role.GetUserID(), role.GetID(), msg.GetIsFinished())

	instanceZone := role.GetInstanceZone()
	if instanceZone == nil || instanceZone.IsEnd {
		node_game.Infoln(role.GetUserID(), role.GetID(), "fail instanceZone is  nil or end")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	if instanceZone.InitType == com_type.InstanceZone_YhBattleGround {
		battleGround := role.GetBattleGround()
		if battleGround == nil {
			node_game.Infoln(role.GetUserID(), role.GetID(), "EndInstanceZone fail :battleGround is nil")
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}

		if !battleGround.ExitCampaign(msg.GetIsFinished()) {
			node_game.Infoln(role.GetUserID(), role.GetID(), "EndInstanceZone fail :battleGround.ExitCampaign fail")
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}

	} else if instanceZone.InitType == com_type.InstanceZone_Mission {
		mission := role.GetRoleMission().GetMissionByCond(instanceZone.ID, com_type.Mission_InstanceZone)
		//判断有没有当前副本的 任务 和 活动
		if mission != nil && msg.GetIsFinished() {
			role.GetRoleMission().AddMissionCond(mission.Id, com_type.Mission_InstanceZone, instanceZone.ID, int32(1))
		}
	} else if instanceZone.InitType == com_type.InstanceZone_Boss {
		if !role.GetRoleBossZone().EndBossZone(msg.GetIsFinished()) {
			node_game.Infoln(role.GetUserID(), role.GetID(), "EndInstanceZone fail bossZone fail")
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}

		//		if msg.GetIsFinished() {
		//			//发放奖励
		//			award := role.GetRoleBossZone().GetCurrentEnter().Award
		//			role.GiveOutAward(award)
		//		}

		if msg.GetIsFinished() {
			role.OnBossZoneDirty()
		}
	}

	instanceZone.IsThroughOK = msg.GetIsFinished()
	instanceZone.IsEnd = true
	if msg.GetIsFinished() {
		exp, gold, awards := instanceZone.GetAllAward()
		role.GiveOutAward(DropPool.PackAwardsToOne(awards))
		role.AddExp(int64(exp))
		role.AddGold(int64(gold))
	} else {
		//	IZDef := table_BattleLevel.Table_.IDMap[insZone.ID]

		//	if IZDef.IsReturnWhenfail {
		//		this.AddGold(int64(insZone.GoldCost))
		//		this.AddDiamond(int64(insZone.DiamondCost))
		//		this.AddActionPoint(int64(insZone.ActionPointCost))
		//		this.AddItem(insZone.ItemCost)

		//	}
	}

	node_game.Infoln(role.GetUserID(), role.GetID(), "EndInstanceZone OK")
	this.errcode = cs_message.EnumType_OK
	this.user.EndTrans(this)
}

func (this *transactionEndInstanceZone) End() {
	this.resp.Errcode = cs_message.EnumType(this.errcode).Enum()
	this.user.Reply(this.req.GetSeriNo(), this.resp)
}

func (this *transactionEndInstanceZone) Timeout() {
	this.errcode = cs_message.EnumType_RETRY
	this.user.EndTrans(this)
}

func init() {
	user.RegisterTransFunc(&cs_message.EndInstanceZoneToS{}, func(user *user.User, msg *codecs.Message) transaction.Transaction {
		return &transactionEndInstanceZone{
			user: user,
			req:  msg,
		}
	})
}
